<?php

namespace app\api\model\music;

use app\logic\model\music\MusicLogicModel;
use app\logic\model\music\MusicSelectedPackLogicModel;
use app\logic\model\user\UserLogicModel;
use app\logic\single\GoodsSingle;

class MusicSelectedPackApiModel extends MusicSelectedPackLogicModel
{

    public function getSelect(array $where = [], string $field = '*', string $order = 'id desc', int $limit = 15)
    {
        return $this->where($where)
            ->field($field)
            ->order($order)
            ->limit($limit)
            ->select();
    }

    public function getToday(array $where = [], string $field = '*', string $order = 'id desc', int $limit = 15)
    {
        {
            return $this->where($where)
                ->field($field)
                ->order($order)
                ->limit($limit)
                ->select();
        }
    }
    public function getApiList(array $param, array $where=[], string $field='*', $page=1, $limit=15,string $order='sort desc')
    {
        if(
            check_request($param,'sortField') && in_array($param['sortField'] , ['create_time','sale','price'])
            && check_request($param,'sortType') && in_array($param['sortType'] , ['desc','asc'])
        ){
            $order = "{$param['sortField']} {$param['sortType']}";
        }
        $query=$this->where($where)
            ->where(function ($q) use ($param){
                $ids_where=[];
                $ids_check=-1;
                if(check_request($param,'title')){
                    $q->whereLike("title","%{$param['title']}%");
                }
                if(check_request($param,'type_id')){
                    $q->whereIn("type_id",$param['type_id']);
                }
                if(check_request($param,'status')){
                    $q->where("status",$param['status']);
                }
                if(check_request($param,'is_pre')){
                    $q->where("is_pre",$param['is_pre']);
                }
                if(check_request($param,'writer_name')){
                    $user_id_arr =UserLogicModel::getIdsByWriterName($param['writer_name']);
                    $music_id_arr = MusicLogicModel::getIdsByUserId($user_id_arr);
                    $sk_id_arr = MusicSelectedPackDetailApiModel::getPackIdByMusicId($music_id_arr);
                    if(!empty($sk_id_arr)){
                        $ids_check = 1;
                        $ids_where =$sk_id_arr;
                    }
                }
                if($ids_check == 1){
                    $q->whereIn("id",array_unique($ids_where));
                }
            });
        $list = $query->field($field)->order($order)
            ->page($page,$limit)->select();
        $count = $query->count();
        if($count>= 1){
            $list = $list->toArray();
            foreach ($list as &$val){
                $val['detail'] = GoodsSingle::getInstance()->getGoods($val['id'],40);
                $val['type'] = $this->getType($val);
            }
        }
        return [$list,$count];
    }
    function getDetail($id,$field='*'){
        $info = GoodsSingle::getInstance()->getGoods($id,40);
        return $info;
    }
    public function details($id,$field='*')
    {
        $info = $this->getFindById($id,$field);
        if(empty($info)){
            return false;
        }
        $info['detail'] = (new MusicSelectedPackDetailApiModel())->getByPackId($id,'music_id,pack_id');

        $info['type'] = $this->getType($info);
        return $info;
    }

}